package com.chl.jdk8.stream;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class StreamDemo {
	public static void main(String[] args) {
//		int[] ns = new int[] { 10, 100, 1000, 10000, 100000, 1000000, 10000000 };
//		for (int n : ns) {
//			testGet(n);
//			testFilter(n);
//			testMap(n);
//		}
		
		
		testMap(10000);
	}

	static void testGet(int n) {
		List<Persion> persions = new ArrayList<>(n);
		initData(n, persions);
		forGet(n, persions);
		streamGet(n, persions);
		System.out.println("persions.size()=" + persions.size());
	}

	static void initData(int n, List<Persion> persions) {
		Persion persion;
		for (int i = 0; i < n; i++) {
			persion = new Persion();
			persion.setName("abc" + i);
			persions.add(persion);
		}
	}

	static void forGet(int n, List<Persion> persions) {
		long b = System.currentTimeMillis();
		for (int i = 0; i < n; i++) {
			persions.get(i).getName();
		}
		long e = System.currentTimeMillis();
		System.out.println(e - b);
	}

	static void streamGet(int n, List<Persion> persions) {
		long b = System.currentTimeMillis();
		persions.stream().forEach(p -> p.getName());
		long e = System.currentTimeMillis();
		System.out.println(e - b);
	}

	static void testFilter(int n) {
		List<Persion> persions = new ArrayList<>(n);
		initData(n, persions);
		forFilter(n, persions);
		streamFilter(n, persions);
		System.out.println("persions.size()=" + persions.size());
	}

	static List<Persion> forFilter(int n, List<Persion> persions) {
		long b = System.currentTimeMillis();
		List<Persion> persions1 = new ArrayList<>();
		Persion p;
		for (int i = 0; i < n; i++) {
			p = persions.get(i);
			if (p.getName().contains("1")) {
				persions1.add(p);
			}
		}
		long e = System.currentTimeMillis();
		System.out.println(e - b);

		return persions1;
	}

	static List<Persion> streamFilter(int n, List<Persion> persions) {
		long b = System.currentTimeMillis();
		List<Persion> persions1 = persions.stream().filter(f -> f.getName().contains("1")).collect(Collectors.toList());
		long e = System.currentTimeMillis();
		System.out.println(e - b);

		return persions1;
	}

	static void forMap(int n, List<Persion> persions) {
		long b = System.currentTimeMillis();
		Map<String, Persion> persions1 = new HashMap<>();
		Persion p;
		for (int i = 0; i < n; i++) {
			p = persions.get(i);
			persions1.put(p.getName(), p);
		}
		long e = System.currentTimeMillis();
		System.out.println(e - b);

	}

	static void streamMap(int n, List<Persion> persions) {
		long b = System.currentTimeMillis();
		Map<String, Persion> persions1 = new HashMap<>();
		persions.forEach(p -> {
			persions1.put(p.getName(), p);
		});
		long e = System.currentTimeMillis();
		System.out.println(e - b);

	}
	
	static void testMap(int n) {
		List<Persion> persions = new ArrayList<>(n);
		initData(n, persions);
//		forMap(n, persions);
		streamMap(n, persions);
		System.out.println("persions.size()=" + persions.size());
	}
}
